package nancal.mp.service.jenkins.publish

import nancal.mp.service.jenkins.JenkinsPublishJob

/**
 * 从Dockerfil镜像中提取Jar
 */
fun JenkinsPublishJob.peek_jar_from_docker(): FunctionContent {
    var logFileName = ".ops.peek-jar-docker.txt"

    return FunctionContent(
        "peek-jar-from-docker", """
${this.appDeploySetting.sudo} chmod a+rw ./ -R &&  rm -rf docker-dir &&  mkdir -p docker-dir && cd docker-dir && echo "文件夹切换到:`pwd`"

${this.appDeploySetting.sudo} touch ${logFileName}
${this.appDeploySetting.sudo} chmod a+rw ${logFileName}

echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "打包镜像:"
echo "${this.appDeploySetting.sudo} docker save -o docker.tar ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version"

${this.appDeploySetting.sudo} docker save -o docker.tar  ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version 
        
if [ ＄? -ne 0 ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '打包镜像错误!!!'
    show-file ${logFileName} 100
   
    exit 1
else
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` '打包镜像成功!'
    show-file ${logFileName} 50
fi

 
echo "解压缩  ${this.appDeploySetting.sudo} tar xf  docker.tar  --warning=no-timestamp "
${this.appDeploySetting.sudo} tar xf  docker.tar  --warning=no-timestamp  && ${this.appDeploySetting.sudo} rm -rf docker.tar
${this.appDeploySetting.sudo} chmod a+rw ./ -R && rm -rf docker_tar_dir && mkdir -p docker_tar_dir

echo "cat manifest.json | curl -X POST -o ${logFileName} -s -w %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${appDeploySetting.mpIntegrationApiUrl}/ops/cmd/get-docker-layers" 
code=＄(cat manifest.json | curl -X POST -o ${logFileName} -s -w  %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${appDeploySetting.mpIntegrationApiUrl}/ops/cmd/get-docker-layers )
 
 
if [ "＄code" == "000" ]; then
    echo "网络错误，请重试！"
    exit 3;
elif [ "＄code" != "200" ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "get-docker-layers接口错误!!! 返回: ＄code"
    show-file ${logFileName} 100
    exit 1
fi


awk 'NR>1{print p}{p=＄0}' ${logFileName} | while read line; do 
    echo "${this.appDeploySetting.sudo} tar xvf ＄line  --warning=no-timestamp  -C docker_tar_dir > /dev/null"
    ${this.appDeploySetting.sudo} tar xvf ＄line  --warning=no-timestamp  -C docker_tar_dir > /dev/null
done

echo "解压缩Docker完成"
 
"""
    )
}


fun JenkinsPublishJob.docker_file_java(): FunctionContent {
    var logFileName = ".ops.docker-file-java.txt"



    return FunctionContent(
        "docker-file-java", """

${this.appDeploySetting.sudo} chmod a+rw ./ -R
cd docker-dir && echo "文件夹切换到:`pwd`"

echo "tree -L 2 -a docker_tar_dir/app"
tree -L 2 -a docker_tar_dir/app



echo "转移目录至: ../docker-app"

echo "mv docker_tar_dir/app ../docker-app"
mv docker_tar_dir/app ../docker-app


echo "${this.appDeploySetting.sudo} docker inspect ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version | curl -X POST -o ${logFileName} -s -w %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${appDeploySetting.mpIntegrationApiUrl}/ops/cmd/get-start-shell"
    
code=＄(${this.appDeploySetting.sudo} docker inspect ${appDeploySetting.harborServer.getHarborImagePrefix(this.task.language)}${this.task.buildInfo.serviceName}:＄docker_image_version | curl -X POST -o ${logFileName}  -s -w %{http_code} --max-time 6 --retry 3 -H"Content-type:text/plain" --data-binary @- ${appDeploySetting.mpIntegrationApiUrl}/ops/cmd/get-start-shell?logId=${this.task.id} )



if [ "＄code" == "000" ]; then
    echo "网络错误，请重试！"
    exit 3;
elif [ "＄code" != "200" ]; then
    echo -e "\n"`date "+%Y-%m-%d %H:%M:%S"` "get-start-shell 接口错误!!! 返回: ＄code"
    show-file ${logFileName} 100
    exit 1
fi

    """
    );
}


fun JenkinsPublishJob.docker_file_vue(): FunctionContent {

    return FunctionContent(
        "docker-file-vue", """
${this.appDeploySetting.sudo} chmod a+rw ./ -R
cd docker-dir && echo "文件夹切换到:`pwd`"

echo "tree -L 2 -a docker_tar_dir/app"
tree -L 2 -a docker_tar_dir/app

echo "转移目录至: ../docker-app"

echo "mv docker_tar_dir/app ../docker-app "
mv docker_tar_dir/app  ../docker-app

echo "mv  docker_tar_dir/etc/nginx/conf.d/default.conf  ../docker-app/default.conf"
mv  docker_tar_dir/etc/nginx/conf.d/default.conf  ../docker-app/default.conf

"""
    );
}
